package com.bitBusy.manageyourriches.database.managers;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;

import com.bitBusy.manageyourriches.categories.initialValues.PaymentMethodEnum;
import com.bitBusy.manageyourriches.categories.initialValues.SpendingCategoryEnum;
import com.bitBusy.manageyourriches.database.queries.QueriesCategoryImagesTable;
import com.bitBusy.manageyourriches.database.queries.QueriesPaymentInfoTable;
import com.bitBusy.manageyourriches.database.queries.QueriesPreviouslyChosenValuesTable;
import com.bitBusy.manageyourriches.database.queries.QueriesSubcategoryCategoryTable;
import com.bitBusy.manageyourriches.database.queries.UpdateSubcategories;
import com.bitBusy.manageyourriches.database.tables.CategoryImages;
import com.bitBusy.manageyourriches.database.tables.PaymentInfo;
import com.bitBusy.manageyourriches.database.tables.PreviouslyChosenValues;
import com.bitBusy.manageyourriches.database.tables.SubcategoryCategory;

/**
 * Class that holds methods to initialize all 
 * tables which have some initial values
 * @author SumaHarsha
 *
 */
public class InitializeTables {
	
	/** Names of tables*/
	private static final String CATEGORYIMAGESTABLE = CategoryImages.class.getSimpleName();
	private static final String SUBCATEGORYCATEGORYTABLE = SubcategoryCategory.class.getSimpleName();
	private static final String PAYMENTINFOTABLE = PaymentInfo.class.getSimpleName();
	private static final String PREVCHOSENVALUESTABLE = PreviouslyChosenValues.class.getSimpleName();
	
	//private static final String BASICDETAILSTABLE = BasicDetails.class.getSimpleName();
	
	/** Array of table names to be initialized with some values*/
	private static final String[] INITIALIZETABLES = 
			new String[]{CATEGORYIMAGESTABLE, SUBCATEGORYCATEGORYTABLE, PAYMENTINFOTABLE, PREVCHOSENVALUESTABLE};

	/** Context reference*/
	Context myContext;

	
	/** Parameterized constructor
	 * @param the current activity context
	 */
	public InitializeTables(Context the_context)
	{
		if (the_context != null)
		{
			myContext = the_context;
		}

	}
		/** Method to initialize the array of tables
		 * @param the_db the database object*/
		public void initalize(SQLiteDatabase the_db)
		{
			if (INITIALIZETABLES != null && INITIALIZETABLES.length > 0 )
			{
			//	myDB = the_db;
				for(int i = 0; i < INITIALIZETABLES.length; i++)
				{
					/*When this table is updated, the subcategories are automatically updated in the SubcategoryCategory
					table and the BasicDetails table*/
					if(CATEGORYIMAGESTABLE.equals(INITIALIZETABLES[i]))
					{
						initializeCategoryImages(the_db);
					}
					else if(SUBCATEGORYCATEGORYTABLE.equals(INITIALIZETABLES[i]))
					{
						initalizeSubcategories(INITIALIZETABLES[i], the_db);
					}
					else if(PAYMENTINFOTABLE.equals(INITIALIZETABLES[i]))
					{
						initializePaymentMethodInfo(the_db);
					}
					else if(PREVCHOSENVALUESTABLE.equals(INITIALIZETABLES[i]))
					{
						initalizePrevChosenValues(the_db);
					}
				}
			}
		}
		
		/** Method to initalize the PreviouslyChosenValues table
		 * @param the database object*/
		private void initalizePrevChosenValues(SQLiteDatabase the_db)
		{
			QueriesPreviouslyChosenValuesTable myQueryObj = new QueriesPreviouslyChosenValuesTable(myContext);
                             		  myQueryObj.addCategories(getCategories(), the_db);
                             		  
		}
		
		/** Method to initialize PaymentInfo table
		 * @param the database object
		 */
		private void initializePaymentMethodInfo(SQLiteDatabase the_db)
		{
			QueriesPaymentInfoTable queryObj = new QueriesPaymentInfoTable(myContext);
			//queryObj.addInitialNames(the_db);
			queryObj.addCategories(getPaymentMethodsAndImages(), the_db);
			
		}
		/** Method to initialize SubcategoryCategories table
		 * @param name of tbale
		 * @param the database object*/
		private void initalizeSubcategories(String the_tableName, SQLiteDatabase the_db)
		{
			UpdateSubcategories queryObj = null;
			
			if(SUBCATEGORYCATEGORYTABLE.equals(the_tableName))
			{
				queryObj = new QueriesSubcategoryCategoryTable(myContext);
			}
			
			if ( queryObj != null)
			{
				for (SpendingCategoryEnum categoryEnum: SpendingCategoryEnum.values())
				{
					queryObj.addSubcategories(categoryEnum.getName(), categoryEnum.getSubcategoryMap(), the_db);
				}
			}
		}
		
	
		/** Method to initalize the CategoryImages table
		 * @param the database object*/
		private void initializeCategoryImages(SQLiteDatabase the_db)
		{
		  QueriesCategoryImagesTable myQueryObj = new QueriesCategoryImagesTable(myContext);
                             		  myQueryObj.addCategories(getCategoryImagesMap(), the_db);			
		}
		
	
		
		/** Method that returns a map of category names and iamges
		 * @return the map
		 */
		
		private Map<String, Integer> getCategoryImagesMap()
		{
			Map<String, Integer> catImgMap = new LinkedHashMap<String, Integer>();
			for (SpendingCategoryEnum categoryEnum: SpendingCategoryEnum.values())
			{
				catImgMap.put(categoryEnum.getName(), categoryEnum.getImgResID());
			}
			return catImgMap;
				
			
		}
		
		/**Method that returns a list of all categories
		 * @return list of category names
		 */
		private List<String> getCategories()
		{
			List<String> categoryList = new ArrayList<String>();
			for (SpendingCategoryEnum categoryEnum: SpendingCategoryEnum.values())
			{
				categoryList.add(categoryEnum.getName());
			
			}
			return categoryList;
			
		}
		
		/** Method that returns a map of category names and subcategory names list
		 * @return the map
		 */
		
	/*	private Map<String, Map<String, Integer>> getCategorySubcategoriesMap()
		{
			Map<String, Map<String, Integer>> catSubcatMap = new LinkedHashMap<String, Map<String, Integer>>();
			
			for (SpendingCategoryEnum categoryEnum: SpendingCategoryEnum.values())
			{
				catSubcatMap.put(categoryEnum.getName(), categoryEnum.getSubcategoryMap());
			}
			return catSubcatMap;
				
			
		}*/
		
		/** Method that returns a map of payment method names and images
		 * @return the map
		 */
		
		private Map<String, Integer> getPaymentMethodsAndImages()
		{
			Map<String, Integer> methImgMap = new LinkedHashMap<String, Integer>();
			for (PaymentMethodEnum method: PaymentMethodEnum.values())
			{
				methImgMap.put(method.getName(), method.getImgID());
			}
			return methImgMap;
				
			
		}
}

		